section	.bss
num	resq	1
wrd	resq	4096

section	.text
global _start
_start:
	call	readword
	cmp	rax,-1
	je	.done
	jmp	_start

.done:
	call	printwords
	call	die

readword
	mov	rbx,[num]
	mov	rax,rbx
	add	rax,1
	mov	[num],rax
	shl	rbx,5 ;; = *32
	lea	rbx,[wrd + rbx]
.loop:
	call	getchar
	cmp	rax,-1
	je	.done
	cmp	rax,' '
	je	.done
	cmp	rax,0x0A
	je	.done
	mov	byte [rbx],al
	inc	rbx
	jmp	.loop
.done
	mov	byte [rbx],0
	ret

printwords:
	mov	rbx,0
	push	rbx

.loop:
	mov	rbx,[rsp]
	mov	rax,rbx
	inc	rax
	mov	[rsp],rax

	mov	rcx,[num]
	cmp	rax,rcx
	je	.done

	shl	rbx,5 ;; = *32
	lea	rbx,[wrd + rbx]
	
	mov	rax,1
	mov	rdi,1
	mov	rsi,rbx
	mov	rdx,32
	syscall
	
	mov	rax,0xA
	call	putchar
	
	jmp	.loop

.done:
	pop	rbx

	ret

die:
	mov	rax,60
	mov	rdi,0
	syscall

%include "getput.asm"
